from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from . import models
from .serializer import *
from django.core.paginator import Paginator
from django.db.models import Q


class DeptarView(APIView):
    def get(self,request):
        # 获取所有有效数据
        deptobj=models.Departtment.objects.filter(status=1).all()

        obj=DeptarSerializer(deptobj,many=True)

        return Response({'code':1000,'msg':'','data':obj.data})



    def post(self,request):
        name=request.POST.get('name','').strip()

        deptobj=models.Staff.objects.filter(name=name).first()
        if deptobj:
            return Response({'code':1003,'msg':'数据已存在'})

        data={
            'name':name,
        }

        obj=DeptarUnserializer(data=data)

        if obj.is_valid():
            flag=obj.save()
            if flag:
                return Response({'code': 1000, 'msg': '添加成功'})
            return Response({'code': 1001, 'msg': '添加失败'})

        print(obj.errors)
        return Response({'code': 1002, 'msg': '参数有误'})



# 员工添加
class StaffView(APIView):

    def post(self,request):
        username=request.POST.get('username','').strip()
        password=request.POST.get('password','').strip()
        dept_id=request.POST.get('depe_id','0')

        staobj=models.Staff.objects.filter(username=username).first()
        if staobj:
            return Response({'code':1002,'msg':'数据已存在','data':''})

        data={
            'username':username,
            'password':password,
            'dept_id':dept_id
        }

        obj=StaffUnserializer(data=data)

        if obj.is_valid():
            flag=obj.save()
            if flag:
                return Response({'code': 1000, 'msg': '添加成功', 'data': ''})
            return Response({'code': 1001, 'msg': '添加失败', 'data': ''})

        print(obj.errors)
        return Response({'code': 1003, 'msg': '参数有误', 'data': ''})


    def get(self,request):
        # 1 接收前端的p值
        p=int(request.GET.get('p',1))
        keyword=request.GET.get('keyword','').strip()
        # 2 获取所有有效的数据  通过姓名模糊查询
        staobj=models.Staff.objects.filter(Q(username__contains=keyword)).all()

        # 3 实列化分页类
        page_obj=Paginator(staobj,per_page=3)

        # 4 获取分页的范围
        page=[i for i in page_obj.page_range]

        # 5 获取当前页数据
        try:
            page_data=page_obj.page(p)
        except:
            page_data = page_obj.page(1)

        # 6 判断上一页和下一页
        previous=page_data.has_previous()
        next=page_data.has_next()

        # 7 序列化当前页数据
        obj=StaffSerializer(page_data,many=True)

        # 8 构造数据
        data={
            'data':obj.data,
            'page':page,
            'previous':previous,
            'next':next
        }

        # 9 返回数据
        return Response({'code':1000,'msg':'','data':data})


class LoginView(APIView):
    def post(self,request):
        username=request.POST.get('username','').strip()
        password = request.POST.get('password', '').strip()

        userobj=models.Staff.objects.filter(username=username,password=password).first()
        if userobj:
            return Response({'code':1000,'msg':'登录成功','data':userobj.id})
        return Response({'code':1001,'msg':'登录失败'})